/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DataAccess.DataAccessFacade;
import DomainModel.ManageEmail;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Mershack
 */
public class PublishGradesController extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet PublishGradesController</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet PublishGradesController at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //  processRequest(request, response);

        //get the emails of the respected students
        try {
            DataAccessFacade dataAccessFacade = DataAccessFacade.getInstance();
            ArrayList<String> emails = new ArrayList<String>();
            
            
            //get the section id from a request parameter, 
             //and get all the email address of students registered for the section
            
            //emails = dataAccessFacade.getStudentEmails(sectionId);
            
            /**sample data to test */
            emails.add("mokoe001@fiu.edu");
            emails.add("prand001@fiu.edu");
            emails.add("reynierov83@gmail.com");
            emails.add("apita011@fiu.edu");
            emails.add("pperi003@fiu.edu");
            emails.add("meshhome16@gmai.com");
            
            
            ManageEmail manageEmail = new ManageEmail();
            
            String messageSubject = "GRS Email Sending Testing";
            String messageDetail = "Hi Guys,\n Prof. Xyz has left a comment on "
                    + "your assignment\n\n This is a test of email sending, which is useful for publishing grades"
                    + " \nregards,\n Mershack";
            
            
            
            boolean success = manageEmail.sendEmail(emails, messageSubject, messageDetail);
            if(success){
                System.out.println("Email sending Successful");
            }
            else{
                System.out.println("Email sending Unsuccessful");
            }
            
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }




    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
